package avicit.bdp.dds.dao.mapper;

import avicit.bdp.common.base.BaseDAO;
import avicit.bdp.common.dto.response.dds.ProcessDefinitionDTO;
import avicit.bdp.common.service.dto.CommonTypeDTO;
import avicit.bdp.dds.api.dto.ProcessDefinitionStateDto;
import avicit.bdp.dds.dao.entity.DefinitionGroupByUser;
import avicit.bdp.dds.dao.entity.ProcessDefinition;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**
 * process definition mapper interface
 */
public interface ProcessDefinitionMapper extends BaseDAO<ProcessDefinition> {


    /**
     * query process definition by name
     *
     * @param projectId projectId
     * @param name      name
     * @return process definition
     */
    ProcessDefinition queryByDefineName(@Param("projectId") String projectId,
                                        @Param("processDefinitionName") String name);

    /**
     * query process definition by id
     *
     * @param processDefineId processDefineId
     * @return process definition
     */
    ProcessDefinition queryByDefineId(@Param("processDefineId") String processDefineId);

    /**
     * process definition page
     *
     * @param page      page
     * @param searchVal searchVal
     * @param userId    userId
     * @param projectId projectId
     * @param isAdmin   isAdmin
     * @return process definition IPage
     */
    Page<ProcessDefinition> queryDefineListPaging(@Param("searchVal") String searchVal,
                                                  @Param("userId") String userId,
                                                  @Param("projectId") String projectId,
                                                  @Param("isAdmin") boolean isAdmin);

    /**
     * query all process definition list
     *
     * @param projectId projectId
     * @return process definition list
     */
    List<ProcessDefinition> queryAllDefinitionList(@Param("projectId") String projectId);

    /**
     * query process definition by ids
     *
     * @param ids ids
     * @return process definition list
     */
    List<ProcessDefinition> queryDefinitionListByIdList(@Param("ids") String[] ids);

    /**
     * query process definition by tenant
     *
     * @param tenantId tenantId
     * @return process definition list
     */
    List<ProcessDefinition> queryDefinitionListByTenant(@Param("tenantId") int tenantId);

    /**
     * count process definition group by user
     *
     * @param userId     userId
     * @param projectIds projectIds
     * @param isAdmin    isAdmin
     * @return process definition list
     */
    List<DefinitionGroupByUser> countDefinitionGroupByUser(
            @Param("userId") String userId,
            @Param("projectIds") String[] projectIds,
            @Param("isAdmin") boolean isAdmin);

    /**
     * list all resource ids
     *
     * @return resource ids list
     */
    @MapKey("id")
    List<Map<String, Object>> listResources();

    /**
     * list all resource ids by user id
     *
     * @return resource ids list
     */
    @MapKey("id")
    List<Map<String, Object>> listResourcesByUser(@Param("userId") String userId);

    /**
     * 更新流程定义附加参数
     *
     * @param id    流程定义ID
     * @param extraParams   附加参数
     */
    void updateProcessDefinitionExtraParams(@Param("id") String id, @Param("extraParams") String extraParams);

    Long getDefinitionByProjectIds(@Param("projectIdList") List<String> projectIdList);

    Long getDefinitionByProjectId(@Param("projectId") String projectId);

    Page<ProcessDefinitionDTO> getDataListPaging(List<String> typeIds, String projectId, Integer type,
                                                 Integer status, String keyWords);

    Integer countDefinitionByName(String projectId, String typeId, String name, Integer type);

    /**
     * 根据项目空间统计流程任务
     * @param userId 当前用户ID
     * @return 流程任务统计列表
     */
    List<ProcessDefinitionStateDto> countDefinitionByProjectId(@Param("userId") String userId);

    /**
     * 根据类型统计流程任务
     * @param userId 当前用户ID
     * @return 流程任务统计列表
     */
    List<ProcessDefinitionStateDto> countDefinitionByType(@Param("userId") String userId);

    ProcessDefinition getDefinitionByProcessInstanceId(@Param("processInstanceId")String processInstanceId);

    List<CommonTypeDTO> getDataDevList(@Param("releaseState") Integer releaseState,
                                       @Param("projectId") String projectId, @Param("type") Integer type,
                                       @Param("keyWords") String keyWords);

    ProcessDefinition selectByIdAndProjectId(@Param("id") String id, @Param("projectId") String projectId);
}
